home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / OT PAPServerSample / PAPServerUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.3 KB  |  168 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        PAPServerUtilities.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Rich Kubota    
  7.  
  8.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/22/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include "PAPServerUtilities.h"
  25. #include <Traps.h>
  26. #include <Files.h>
  27. #include <Errors.h>
  28. #include <Devices.h>
  29. /* the following code is from IM VI 3-8 which demonstrates how to check for the 
  30.  * availability of traps
  31.  */
  32.  
  33. short NumToolboxTraps(void)
  34. {
  35.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  36.         return(0x200);
  37.     else
  38.         return(0x400);
  39.  
  40. } /* NumToolboxTraps */
  41.  
  42. TrapType GetTrapType(short theTrap)
  43. {
  44.     short    trapMask = 0x0800;
  45.     
  46.     if (theTrap & trapMask)
  47.         return(ToolTrap);
  48.     else
  49.         return(OSTrap);
  50.  
  51. } /* GetTrapType */
  52.  
  53. Boolean TrapAvailable(short theTrap)
  54. {
  55.     TrapType    theType;
  56.     
  57.     theType = GetTrapType(theTrap);
  58.     if (theType == ToolTrap) {
  59.         theTrap &= 0x07FF;
  60.         if (theTrap >= NumToolboxTraps())
  61.             theTrap = _Unimplemented;
  62.     }
  63.     return(NGetTrapAddress(theTrap, theType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  64. }
  65.  
  66. /***************************************************************************
  67. The following function was taken from the MoreFiles.c DTS Sample code.
  68. **/
  69.  
  70. pascal    OSErr    HCreateMinimum(short vRefNum,
  71.                                long dirID,
  72.                                ConstStr255Param fileName)
  73. {
  74.     HParamBlockRec pb;
  75.  
  76.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  77.     pb.fileParam.ioVRefNum = vRefNum;
  78.     pb.ioParam.ioVersNum = 0;
  79.     pb.fileParam.ioDirID = dirID;
  80.     return ( PBHCreateSync(&pb) );
  81. }
  82.  
  83.  
  84. /* 
  85.  * OpenTempFile opens a temporary file on the root volume of the boot drive
  86.  * returns the file ref num to write to, or -1 if a problem occurred
  87.  *
  88.  * This code is not meant to demonstrate how one goes about creating a temporary
  89.  * file, it's more a matter of expediency for me.
  90.  */
  91. OSErr    OpenTempFile(short    *fRefNum)
  92. {
  93.     HParamBlockRec    pb;
  94.     short            index = 0;
  95.     OSErr            err;
  96.     
  97.     Str255            fname = "\pSavedPAPFile";
  98.                         //       123456789012        temp file name is 12 characters long
  99.     
  100.         // initialize err as dupFNErr since that and noErr are the only errors
  101.         // which we can deal with here
  102.     err = dupFNErr;
  103.     while (err == dupFNErr)
  104.     {
  105.         if (index < 10)
  106.         {
  107.             fname[0] = 14;    // specify that the file name is 14 character pascal string
  108.             fname[14] = '0' + index;
  109.         }
  110.         else if (index < 100)    // temp files 'SavedPAPFile 0' - 'SavedPAPFile 9' already exist
  111.         {
  112.             fname[0] = 15;
  113.             fname[14] = '0' + (index / 10);        // set the tens placeholder
  114.             fname[15] = '0' + (index % 10);        // set the units placeholder
  115.         }
  116.         else // this part of the code will likely not be tested, but it's implemented just in case.
  117.         {
  118.             fname[0] = 16;
  119.             fname[14] = '0' + (index / 100);        // set the hundreds placeholder
  120.             fname[15] = '0' + ((index % 100) / 10);    // set the tens placeholder
  121.             fname[16] = '0' + (index % 10);            // set the units placeholder
  122.         }
  123.         
  124.         index++;      // increment index for the next go around;
  125.         
  126.         err = HCreateMinimum(kBootVolVRefNum, kRootFolderDirID, fname);
  127.     }
  128.     
  129.         // was the file created successfully
  130.         
  131.     if (err == noErr)
  132.     {
  133.         pb.fileParam.ioNamePtr = (StringPtr) fname;
  134.         pb.ioParam.ioPermssn = fsWrPerm;
  135.         pb.ioParam.ioMisc = 0;
  136.         pb.fileParam.ioDirID = kRootFolderDirID;
  137.         pb.fileParam.ioVRefNum = kBootVolVRefNum;
  138.         err = PBHOpenSync(&pb);
  139.     }
  140.     
  141.     if (err != noErr)
  142.         *fRefNum = 0;
  143.     else
  144.         *fRefNum = pb.ioParam.ioRefNum;
  145.         
  146.     return err;
  147. }
  148.  
  149.  
  150. OSErr WriteDataToTempFile(short fRefNum, UInt8 *buffer, UInt32 len)
  151. {
  152.     HParamBlockRec    pb;
  153.  
  154.     pb.ioParam.ioRefNum = fRefNum;
  155.     pb.ioParam.ioBuffer = (Ptr)buffer;
  156.     pb.ioParam.ioReqCount = len;
  157.     pb.ioParam.ioPosMode = fsAtMark + 0x0020;  /* fsAtMarK + noCacheBit */
  158.     pb.ioParam.ioPosOffset = 0;
  159.     return (PBWriteSync((ParmBlkPtr)&pb));    
  160. }
  161.  
  162. OSErr CloseTempFile(short fRefNum)
  163. {
  164.     HParamBlockRec    pb;
  165.  
  166.     pb.ioParam.ioRefNum = fRefNum;
  167.     return (PBCloseSync((ParmBlkPtr)&pb));    
  168. }